/**
 * Independent time operation tool to facilitate subsequent switch to dayjs
 */
import dayjs, { Dayjs } from 'dayjs';

const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss';
const DATE_FORMAT = 'YYYY-MM-DD';

export function formatToDateTime(date: dayjs.Dayjs | undefined = undefined, format = DATE_TIME_FORMAT): string {
  return dayjs(date).format(format);
}

export function formatToDate(date: dayjs.Dayjs | undefined = undefined, format = DATE_FORMAT): string {
  return dayjs(date).format(format);
}

export const dateUtil = dayjs;

/**
 * 格式化日期范围
 * @param dateRange - 日期范围数组
 * @returns 格式化后的日期范围字符串
 */
export const dateRangeFormatter = (dateRange: [Dayjs, Dayjs]): string => {
  let [date1, date2] = dateRange;

  if (date1.isSame(date2, "day")) {
    if (date1.isSame(dayjs(), "year")) {
      return date1.format("M月D日");
    } else {
      return date1.format("YYYY年M月D日");
    }
  } else {
    if (date1.isSame(dayjs(), "year")) {
      return date1.format("M月D日") + " - " + date2.format("M月D日");
    }
    return date1.format("YYYY年M月D日") + " - " + date2.format("YYYY年M月D日");
  }
};

/**
 * 格式化单个日期
 * @param date - 日期对象
 * @returns 格式化后的日期字符串
 */
export const dateFormatter = (date: Dayjs): string => {
  if (date.isSame(dayjs(), "year")) {
    return date.format("M月D日");
  }
  return date.format("YYYY年M月D日");
};

/**
 * 格式化月份范围
 * @param beginMonth - 开始月份
 * @param endMonth - 结束月份
 * @returns 格式化后的月份范围字符串
 */
export const monthRangeFormatter = (beginMonth: Dayjs, endMonth: Dayjs): string => {
  if (beginMonth.isSame(endMonth, "month")) {
    return beginMonth.format("YYYY年M月");
  }
  return `${beginMonth.format("YYYY年M月")} - ${endMonth.format("YYYY年M月")}`;
};

/**
 * 格式化单个月份
 * @param date - 日期对象
 * @returns 格式化后的月份字符串
 */
export const monthFormatter = (date: Dayjs): string => {
  if (date.isSame(dayjs(), "year")) {
    return date.format("M月");
  }
  return date.format("YYYY年M月");
};

/**
 * 生成年份列表（当前年份前后10年）
 * @returns 年份数组
 */
export const generateYearList = (): number[] => {
  const currentYear = dayjs().year();
  const yearList: number[] = [];
  for (let i = currentYear - 10; i <= currentYear + 10; i++) {
    yearList.push(i);
  }
  return yearList;
};

interface MonthItem {
  value: string;
  name: string;
}

/**
 * 生成月份列表（1-12月）
 * @returns 月份对象数组
 */
export const generateMonthList = (): MonthItem[] => {
  const monthsArray: MonthItem[] = [];
  for (let i = 1; i <= 12; i++) {
    const value = i < 10 ? `0${i}` : `${i}`;
    const name = `${i}月`;
    monthsArray.push({ value, name });
  }
  return monthsArray;
};

interface QuarterItem {
  value: number;
  name: string;
}

/**
 * 生成季度列表（Q1-Q4）
 * @returns 季度对象数组
 */
export const generateQuarterList = (): QuarterItem[] => {
  const quarterArray: QuarterItem[] = [];
  for (let i = 1; i <= 4; i++) {
    const value = i;
    const name = `Q${i}`;
    quarterArray.push({ value, name });
  }
  return quarterArray;
};

/**
 * 生成周列表（1-5周）
 * @returns 周数组
 */
export const generateWeekList = (): number[] => {
  const monthsArray: number[] = [];
  for (let i = 1; i <= 5; i++) {
    monthsArray.push(i);
  }
  return monthsArray;
};

/**
 * 根据月份获取所属季度
 * @param month - 月份（1-12）
 * @returns 季度（1-4）
 * @throws 如果月份无效则抛出错误
 */
export const getQuarter = (month: number): number => {
  if (month < 1 || month > 12) {
    throw new Error("Invalid month. Please enter a value between 1 and 12.");
  }
  return Math.ceil(month / 3);
};
